<template>
  <a-spin :spinning="loading">
    <a-tabs v-model:activeKey="resume"  animated style="margin-left: 20px">
      <a-tab-pane tab="简历" key="resume" :forceRender="true" >
    <a-form v-bind="formItemLayout">
      <a-row>
        <a-col :span="12">
          <a-form-item label="所属公司" v-bind="validateInfos.supplier">
	          <j-dict-select-tag v-model:value="formData.supplier" dictCode="tm_supplier_manage,supplier_name,id" placeholder="请选择所属公司" :disabled="disabled"/>
            <!-- <j-search-select v-model:value="formData.supplier" dict="tm_supplier_manage,supplier_name,id" placeholder="请选择项所属公司" :disabled="disabled" allowClear/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="姓名" v-bind="validateInfos.realName">
            <a-input v-model:value="formData.realName" placeholder="请输入姓名" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="身份证号" v-bind="validateInfos.idNo">
            <a-input v-model:value="formData.idNo" placeholder="请输入身份证号" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="性别" v-bind="validateInfos.jender">
	          <j-dict-select-tag v-model:value="formData.jender" dictCode="sex" placeholder="请选择性别" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="电话" v-bind="validateInfos.mobile">
            <a-input v-model:value="formData.mobile" placeholder="请输入电话" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="邮箱" v-bind="validateInfos.email">
            <a-input v-model:value="formData.email" placeholder="请输入邮箱" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="出生日期" v-bind="validateInfos.birthday">
		        <a-date-picker placeholder="请选择出生日期" v-model:value="formData.birthday" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="学历" v-bind="validateInfos.education">
	          <j-dict-select-tag v-model:value="formData.education" dictCode="education" placeholder="请选择学历" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="专业" v-bind="validateInfos.major">
            <a-input v-model:value="formData.major" placeholder="请输入专业" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工作年限" v-bind="validateInfos.workYears">
	          <a-input-number v-model:value="formData.workYears" placeholder="请输入工作年限" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="技能项" v-bind="validateInfos.skill">
	          <j-select-multiple type="list_multi" v-model:value="formData.skill" dictCode="skill_list" placeholder="请选择技能项" :disabled="disabled" :triggerChange="false" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="期望工作地点" v-bind="validateInfos.workPlace">
            <!-- <j-select-multiple type="list_multi" v-model:value="formData.userId" dictCode="sys_user,realname,username" placeholder="请选择成员" :disabled="disabled" :triggerChange="false"/> -->
            <j-select-multiple type="list_multi" v-model:value="formData.workPlace" dictCode="tm_work_place,work_place,id" placeholder="请选择期望工作地点" :disabled="disabled"  :triggerChange="false" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="简历附件" v-bind="validateInfos.resumeFile">
	          <j-upload v-model:value="formData.resumeFile"  :disabled="disabled" allowClear></j-upload>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="人员需求">
	          <j-dict-select-tag v-model:value="formData.positionReqId" dictCode="tm_position_req,position_req_no,id" placeholder="请选择人员需求" :disabled="disabled"/>
          </a-form-item>
        </a-col> -->
  
        <a-col :span="24">
<!--        </a-col>-->
<!--        <a-col  style="margin-left: -180px;width: 1450px">-->
<!--          <a-form-item label="简历内容" v-bind="validateInfos.resumeContentString" style="width: 1450px">-->
<!--        <a-col style="margin-left: -180px;width: 1450px">-->
          <a-form-item label="简历内容" v-bind="validateInfos.resumeContentString">
	          <j-editor v-model:value="formData.resumeContentString" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
      </a-tab-pane>
		<!-- 子表单区域 -->

      <a-tab-pane tab="面试评价" key="resumeEvaluateRecord" :forceRender="true">
        <j-vxe-table
          :keep-source="true"
          resizable
          ref="resumeEvaluateRecordTableRef"
          :loading="resumeEvaluateRecordTable.loading"
          :columns="resumeEvaluateRecordTable.columns"
          :dataSource="resumeEvaluateRecordTable.dataSource"
          :height="340"
          disabled="true"
          :rowNumber="true"/>
      </a-tab-pane>
      <a-tab-pane tab="入场信息" key="projectEntryDetail" :forceRender="true">
        <j-vxe-table
          :keep-source="true"
          resizable
          ref="projectEntryDetailTableRef"
          :loading="projectEntryDetailTable.loading"
          :columns="projectEntryDetailTable.columns"
          :dataSource="projectEntryDetailTable.dataSource"
          :height="340"
          disabled="true"
          :rowNumber="true"/>
      </a-tab-pane>
      <a-tab-pane tab="离场评价" key="departureEvaluateRecord" :forceRender="true">
        <j-vxe-table
          :keep-source="true"
          resizable
          ref="departureEvaluateRecordTableRef"
          :loading="departureEvaluateRecordTable.loading"
          :columns="departureEvaluateRecordTable.columns"
          :dataSource="departureEvaluateRecordTable.dataSource"
          :height="340"
          disabled="true"
          :rowNumber="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, computed, toRaw, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useValidateAntFormAndTable } from '/@/hooks/system/useJvxeMethods';
  import { opertResumeFilter,queryResumeEvaluateRecordListByMainId, queryProjectEntryDetailListByMainId, queryDepartureEvaluateRecordListByMainId, queryDataById, saveOrUpdate } from '../Resume.api';
  import { JVxeTable } from '/@/components/jeecg/JVxeTable';
  import {resumeEvaluateRecordColumns, projectEntryDetailColumns, departureEvaluateRecordColumns} from '../Resume.data';
  // import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  import { Form } from 'ant-design-vue';
  const useForm = Form.useForm;

  export default defineComponent({
    name: "ResumeForm",
    components:{
      JDictSelectTag,
      JSelectMultiple,
      JUpload,
      JEditor,
      JVxeTable,
    },
    props:{
      formDisabled:{
        type: Boolean,
        default: false
      },
      formData: { type: Object, default: ()=>{} },
      formBpm: { type: Boolean, default: true }
    },
    emits:['success'],
    setup(props, {emit}) {
      const loading = ref(false);
      const resumeEvaluateRecordTableRef = ref();
      const resumeEvaluateRecordTable = reactive<Record<string, any>>({
        loading: false,
        columns: resumeEvaluateRecordColumns,
        dataSource: []
      });
      const projectEntryDetailTableRef = ref();
      const projectEntryDetailTable = reactive<Record<string, any>>({
        loading: false,
        columns: projectEntryDetailColumns,
        dataSource: []
      });
      const departureEvaluateRecordTableRef = ref();
      const departureEvaluateRecordTable = reactive<Record<string, any>>({
        loading: false,
        columns: departureEvaluateRecordColumns,
        dataSource: []
      });
      const activeKey = ref('resumeEvaluateRecord');
      const formData = reactive<Record<string, any>>({
        id: '',
        supplier: '',
        realName: '',
        idNo: '',
        jender: '',
        mobile: '',
        email: '',
        birthday: '',
        education: '',
        major: '',
        workYears: undefined,
        skill: '',
        workPlace:'',
        resumeFile: '',
        resumeContentString: '',
        filterStatus:'',
        positionReqId:''
      });

      //表单验证
      const validatorRules = reactive({
        supplier: [{ required: true, message: '请输入所属公司!'},],
        realName: [{ required: true, message: '请输入姓名!'},],
        // idNo: [{ required: true, message: '请输入身份证号!'}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
        jender: [{ required: true, message: '请输入性别!'},],
        // mobile: [{ required: true, message: '请输入电话!'}, { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},],
        // email: [{ required: true, message: '请输入邮箱!'}, { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},],
        // birthday: [{ required: true, message: '请输入出生日期!'},],
        education: [{ required: true, message: '请输入学历!'},],
        major: [{ required: true, message: '请输入专业!'},],
        workYears: [{ required: true, message: '请输入工作年限!'}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
        skill: [{ required: true, message: '请输入技能项!'},],
        resumeFile: [{ required: true, message: '请输入简历附件!'},],
        resumeContent: [{ required: true, message: '请输入简历内容!'},],
      });
      const {resetFields, validate, validateInfos} = useForm(formData, validatorRules, {immediate: false});
      const dbData = {};
      const formItemLayout = {
        // labelCol: {xs: {span: 24}, sm: {span: 5}},
        // wrapperCol: {xs: {span: 24}, sm: {span: 16}},
        labelCol: {style: { width: '100px' }},
        wrapperCol: {},
      };

      // 表单禁用
      const disabled = computed(()=>{
        if(props.formBpm === true){
          if(props.formData.disabled === false){
            return false;
          }else{
            return true;
          }
        }
        return props.formDisabled;
      });



      function add() {
        resetFields();
        resumeEvaluateRecordTable.dataSource = [];
        projectEntryDetailTable.dataSource = [];
        departureEvaluateRecordTable.dataSource = [];
      }

      async function edit(row) {
        //主表数据
        await queryMainData(row.id);
        //子表数据
        const resumeEvaluateRecordDataList = await queryResumeEvaluateRecordListByMainId(row['id']);
        resumeEvaluateRecordTable.dataSource = [...resumeEvaluateRecordDataList];
        const projectEntryDetailDataList = await queryProjectEntryDetailListByMainId(row['id']);
        projectEntryDetailTable.dataSource = [...projectEntryDetailDataList];
        const departureEvaluateRecordDataList = await queryDepartureEvaluateRecordListByMainId(row['id']);
        departureEvaluateRecordTable.dataSource = [...departureEvaluateRecordDataList];
      }

      async function queryMainData(id) {
        const row = await queryDataById(id);
        const tmpData = {};
        Object.keys(formData).forEach((key) => {
          if(row.hasOwnProperty(key)){
            tmpData[key] = row[key]
          }
        })
        //赋值
        Object.assign(formData,tmpData);
      }

      const {getSubFormAndTableData, transformData} = useValidateAntFormAndTable(activeKey, {
        'resumeEvaluateRecord': resumeEvaluateRecordTableRef,
        'projectEntryDetail': projectEntryDetailTableRef,
        'departureEvaluateRecord': departureEvaluateRecordTableRef,
      });

      async function getFormData() {
        await validate();
        return transformData(toRaw(formData))
      }

      async function submitForm() {
        const mainData = await getFormData();
        const subData = await getSubFormAndTableData();
        const values = Object.assign({}, dbData, mainData, subData);
        const isUpdate = values.id ? true : false
        await saveOrUpdate(values, isUpdate);
        //关闭弹窗
        emit('success');
      }

      async function handleResumeFilter() {
        const mainData = await getFormData();
        const subData = await getSubFormAndTableData();
        const values = Object.assign({}, dbData, mainData, subData);
        await opertResumeFilter(values);
        //关闭弹窗
        emit('success');
      }


      function setFieldsValue(values) {
        if(values){
          Object.keys(values).map(k=>{
            formData[k] = values[k];
          });
        }
      }

      /**
       * 值改变事件触发-树控件回调
       * @param key
       * @param value
       */
      function handleFormChange(key, value) {
        formData[key] = value;
      }


      return {
        resumeEvaluateRecordTableRef,
        resumeEvaluateRecordTable,
        projectEntryDetailTableRef,
        projectEntryDetailTable,
        departureEvaluateRecordTableRef,
        departureEvaluateRecordTable,
        validatorRules,
        validateInfos,
        activeKey,
        loading,
        formData,
        setFieldsValue,
        handleFormChange,
        formItemLayout,
        disabled,
        getFormData,
        submitForm,
        handleResumeFilter,
        add,
        edit
      }
    }
  });
</script>
